﻿#include "threadshellutils.h"
#include "appcommon.h"

#include "util/util.h"

#include <QFile>
ThreadShellUtils::ThreadShellUtils(QObject *parent) :
    QThread(parent) {
}

bool ThreadShellUtils::runShell(const QString &shellFilePath, const QStringList &args,
                                const QString &grepPattern, int timeout) {
    if (AppCommon::isNullOrEmpty(shellFilePath)) {
        return false;
    }
    QFile file(shellFilePath);
    if (!file.exists()) {
        return false;
    }
    if (!m_mutex.tryLock(1000)) {
        return false;
    }
    m_shellParams.clear();
    QStringList shell_args;
    shell_args << shellFilePath;
    shell_args << args;

    m_shellParams["command"] = "/bin/bash";
    m_shellParams["args"] = shell_args;
    m_shellParams["grepPattern"] = grepPattern;
    m_shellParams["timeout"] = timeout;
    m_mutex.unlock();
    this->start();
    return true;
}

bool ThreadShellUtils::runCommand(const QString &command, const QStringList &args,
                                  const QString &grepPattern, int timeout) {
    if (AppCommon::isNullOrEmpty(command)) {
        return false;
    }
    if (!m_mutex.tryLock(1000)) {
        return false;
    }
    m_shellParams.clear();
    m_shellParams["command"] = "/bin/bash";
    m_shellParams["args"] = args;
    m_shellParams["grepPattern"] = grepPattern;
    m_shellParams["timeout"] = timeout;
    m_mutex.unlock();
    this->start();
    return true;
}

bool ThreadShellUtils::runCommand(const QStringList &command, const QString &grepPattern,
                                  int timeout) {
    if (command.size() <= 0) {
        return false;
    }
    if (!m_mutex.tryLock(1000)) {
        return false;
    }
    m_shellParams.clear();
    m_shellParams["command"] = command.at(0);
    if (command.size() >= 2) {
        QStringList args = command.mid(1, command.size() - 1);
        m_shellParams["args"] = args;
    }
    m_shellParams["grepPattern"] = grepPattern;
    m_shellParams["timeout"] = timeout;
    m_mutex.unlock();
    this->start();
    return true;
}

bool ThreadShellUtils::ping(QString ip, int timeout, int times) {
    if (AppCommon::isNullOrEmpty(ip)) {
        return false;
    }
    if (!m_mutex.tryLock(1000)) {
        return false;
    }
    m_shellParams.clear();
    m_shellParams["command"] = "ping";
    QStringList args;
    args << ip;

#ifdef CPPC_OS_LINUX
    args << "-W";
    args << QString::number(timeout);
    args << "-c";
    args << QString::number(times);
#else
    args << "-w";                            // Timeout in milliseconds
    args << QString::number(timeout * 1000); // Convert seconds to milliseconds
    args << "-n";                            // Number of echo requests to send
    args << QString::number(times);
#endif

    m_shellParams["args"] = args;

    m_mutex.unlock();
    this->start();
    return true;
}

void ThreadShellUtils::run() {
    QMutexLocker lock(&m_mutex);
    if (!m_shellParams.contains("command")) {
        emit sigShellRunEnd(ShellUtils::ShellResult::errorResult(tr("待执行命令为空"), 0));
        return;
    }
    QString command = m_shellParams["command"].toString();
    QStringList args = m_shellParams["args"].toStringList();
    QString grepPattern = m_shellParams["grepPattern"].toString();
    int timeout = m_shellParams["timeout"].toInt();
    ShellUtils::ShellResult result = ShellUtils::runCommand(command, args, grepPattern, timeout);
    emit sigShellRunEnd(result);
}
